﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Publish_WriteComment : System.Web.UI.Page
{
    protected string m_title = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        string id = "";
        if (Request.Params["id"] != null)
            id = Request.Params["id"].ToString();

        if (!Page.IsPostBack)
        {
            GssDO.DOProduct DO = getById(id);
            if (DO != null && DO.IsDeleted == 0)
            {
                m_title = DO.Title;
                txtTitle.Text = "Nhận xét: " + m_title;
            }
        }
    }

    private bool validation()
    {
        bool result = true;

        if (!reqFullname.IsValid)
            result = false;
        if (!reqEmail.IsValid)
            result = false;
        if (!reqEmailFormat.IsValid)
            result = false;
        if (!reqTitle.IsValid)
            result = false;
        if (!reqContent.IsValid)
            result = false;
        else
        {
            string content = txtContent.Text.Trim();
            content = NameParser.HTMLParser.RemoveHTMLTags(content);
            if (content.Length > 1000)
            {
                reqContent.IsValid = false;
                result = false;
            }
        }
        if (!reqAuthentic.IsValid)
            result = false;
        else if (Session["authentic"] == null)
        {
            reqAuthentic.IsValid = false;
            result = false;
        }
        else if (txtAuthentic.Text.Trim() != Session["authentic"].ToString())
        {
            reqAuthentic.IsValid = false;
            result = false;
        }

        return result;
    }

    private GssDO.DOComment fillDO()
    {
        string id = "";
        if (Request.Params["id"] != null)
            id = Request.Params["id"].ToString();
        GssDO.DOComment DO = new GssDO.DOComment();

        DO.PKIDComment = Guid.NewGuid().ToString();
        DO.ProductID = id;
        DO.CreatedDate = DateTime.Now;
        DO.Fullname = NameParser.HTMLParser.RemoveHTMLTags(txtFullname.Text.Trim());
        DO.Email = NameParser.HTMLParser.RemoveHTMLTags(txtEmail.Text.Trim());
        DO.Title = NameParser.HTMLParser.RemoveHTMLTags(txtTitle.Text.Trim());
        DO.Content = NameParser.HTMLParser.RemoveHTMLTags(txtContent.Text.Trim());
        if (ckbEmail.Checked)
            DO.IsEmail = 1;
        else
            DO.IsEmail = 0;
        DO.IsProcess = 0;
        DO.IsShow = 0;
        DO.Level = 0;
        DO.IsDeleted = 0;

        return DO;
    }

    private void sendComment()
    {
        if (validation())
        {
            try
            {
                GssDO.DOComment DO = fillDO();
                GssBL.BLComment BL = new GssBL.BLComment();

                BL.Insert(DO);
                Response.Redirect(@"WriteCommentSuccess.aspx", false);
            }
            catch (Exception exc)
            {
                string mess = exc.Message.ToString();
            }
        }
    }

    protected void btnSend_ServerClick(object sender, EventArgs e)
    {
        sendComment();
    }
    protected void btnReset_ServerClick(object sender, EventArgs e)
    {
        string id = "";
        if (Request.Params["id"] != null)
            id = Request.Params["id"].ToString();

        Response.Redirect(@"WriteComment.aspx?id=" + id, false);
    }

    private GssDO.DOProduct getById(string id)
    {
        GssDO.DOProduct DO = null;
        try
        {
            GssBL.BLProduct BL = new GssBL.BLProduct();
            DO = BL.getProductByID(id);
        }
        catch (Exception ex)
        {
            string mess = ex.Message.ToString();
            DO = null;
        }
        return DO;
    }
}
